//
//  MegafaunaOfTheWeekAsyncImageView.m
//  Cachalot
//
//  Created by Adam Cue on 6/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MegafaunaOfTheWeekAsyncImageView.h"

#define IMAGE_HEIGHT 492

@implementation MegafaunaOfTheWeekAsyncImageView

@synthesize info = _info;
@synthesize delegate;
@synthesize index;

static CGFloat kDefaultReflectionOpacity = 0.30;

#pragma mark ExternalMethods
- (void)setSpinnerForLoading{
   for(UIView *view in self.subviews){
      [view removeFromSuperview];
   }
   UIActivityIndicatorView * spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
   spinner.center = self.center;
   [spinner startAnimating];
   [self addSubview:spinner];
   [self bringSubviewToFront:spinner];
   [spinner release];
}

#pragma mark AsyncImage Overrides
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
   [connection release];
	connection=nil;
	for(UIView *view in self.subviews){
		[view removeFromSuperview];
	}
	NSData *image = UIImageJPEGRepresentation([UIImage imageWithData:data], 1.0);
	UIImageView* imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, IMAGE_HEIGHT)] autorelease];
	imageView.contentMode = UIViewContentModeScaleAspectFill;
   imageView.image = [UIImage imageWithData:image];
   imageView.clipsToBounds = YES;
	[self addSubview:imageView];
	
   UIImageView * reflectedImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, imageView.frame.size.height, imageView.frame.size.width, imageView.frame.size.height)] autorelease];
   reflectedImage.contentMode = UIViewContentModeScaleAspectFill;
	reflectedImage.image = [self reflectedImage:imageView.image withWidth:imageView.image.size.width andHeight:imageView.image.size.height];
   reflectedImage.clipsToBounds = YES;
	reflectedImage.alpha = kDefaultReflectionOpacity;
   [self addSubview:reflectedImage];
	[data release];
	data=nil;
   if(delegate) [delegate imageView:self downloadedImage:[self image] withIndex:index andInfo:self.info];
}

- (void)setDisplayImage:(UIImage *)selectedImage {
   for(UIView *view in self.subviews){
		[view removeFromSuperview];
	}
	UIImageView* imageView = [[[UIImageView alloc] initWithImage:selectedImage] autorelease];
	imageView.contentMode = UIViewContentModeScaleAspectFill;
   imageView.clipsToBounds = YES;
	[self addSubview:imageView];
	imageView.frame = CGRectMake(0, 0, self.frame.size.width, IMAGE_HEIGHT);
	
   UIImageView * reflectedImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, imageView.frame.size.height, imageView.frame.size.width, imageView.frame.size.height)] autorelease];
	reflectedImage.image = [self reflectedImage:imageView.image withWidth:imageView.image.size.width andHeight:imageView.image.size.height];
   reflectedImage.contentMode = UIViewContentModeScaleAspectFill;
   reflectedImage.clipsToBounds = YES;
	reflectedImage.alpha = kDefaultReflectionOpacity;
   [self addSubview:reflectedImage];
}

@end
